import wx
from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor
from vtk import vtkInteractorStyleTrackballCamera
from Sensor import Sensor
import os

##  Subpanel of VisualizationPanel which shows the real time movement using the VTK window. New recordings can also be made in this panel.
class RecordingPanel(wx.Panel):

    ##  The constructor of RecordingPanel. GUI elements of the panel are setup here.
    #   @param[in]  parent  The wx window parent (VisualizationState) of this panel.
    #   @param[in]  state   RecordingState instance attached to this panel.
    def __init__(self, parent, state):
        wx.Panel.__init__(self, parent)
        ##  The name of this notebook panel.
        self.name = "Recording"
        ##  RecordingState instance attached to this panel.
        self.state = state
        self.state.panel = self
        self.lastpath = ""

        ##  wxVTKRenderWindowInteractor used to show the 3D model.
        self.widget = wxVTKRenderWindowInteractor(self, -1)
        self.widget.SetInteractorStyle(vtkInteractorStyleTrackballCamera())
        self.widget.GetRenderWindow().AddRenderer(self.state.ren)
        if self.state.main.Smooth3DModel:
            self.widget.GetRenderWindow().SetAAFrames(3)
        ##  Starts a recording.
        self.recordButton = wx.BitmapButton(self, -1, wx.Bitmap("Images/Record.png", wx.BITMAP_TYPE_PNG))
        ##  Label indicating whether you are recording or not.
        self.recordLabel = wx.StaticText(self, -1, "")

        ##  Reset Camera button.
        self.resetCamButton = wx.Button(self, -1, "Reset Camera")

        # Set properties.
        self.widget.SetSize(self.widget.GetBestSize())
        self.recordButton.SetMinSize((75, 25))

        # Set sizers.
        realTimeSizer = wx.BoxSizer(wx.HORIZONTAL)
        realTimeGrid = wx.FlexGridSizer(2, 1, 10, 0)

        buttonSizer = wx.FlexGridSizer(2, 2, 0, 5)
        buttonSizer.Add(self.resetCamButton, 0, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 10)
        buttonSizer.Add(self.recordButton, 0, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 10)
        buttonSizer.Add(wx.StaticText(self, -1, ""),0, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 10)
        buttonSizer.Add(self.recordLabel, 0, wx.ALIGN_CENTER_VERTICAL, 0)


        realTimeGrid.Add(self.widget, 0, wx.ALL|wx.EXPAND, 10)
        realTimeGrid.Add(buttonSizer, 1, wx.ALIGN_RIGHT, 0)

        realTimeGrid.AddGrowableRow(0)
        realTimeGrid.AddGrowableCol(0)
        realTimeSizer.Add(realTimeGrid, 1, wx.EXPAND, 0)
        self.SetSizer(realTimeSizer)

        self.Bind(wx.EVT_BUTTON, self.OnRecord, self.recordButton)
        self.Bind(wx.EVT_BUTTON, self.OnResetCam, self.resetCamButton)
        self.Layout()

    ##  Event handler for the timer, stops timer.
    def OnTimer(self, event):
        self.recordLabel.SetLabel("")

    ##  Event handler for the Record button, toggles the recording on and off in visualizationState.
    def OnRecord(self, event):
        if self.state.RECORDING:
            self.recordButton.SetBitmapLabel(wx.Bitmap(self.state.main.config.root+"\\Images\\Record.png", wx.BITMAP_TYPE_PNG))
            self.recordLabel.SetLabel("Recording Completed")
        else:
            self.recordButton.SetBitmapLabel(wx.Bitmap(self.state.main.config.root+"\\Images\\Stop.png", wx.BITMAP_TYPE_PNG))
            self.recordLabel.SetLabel("Recording...")

        self.Layout()
        self.OnToggleRecord()

    ##  Toggles the recording of movement on and off.
    def OnToggleRecord(self):
        if self.state.RECORDING:
            #~ self.state.main.driver.fileWriter.close()
            
            self.state.RECORDING = False
            dlg = wx.FileDialog(self.state.main.gui, message="Save as FoB file", defaultDir=os.getcwd(), defaultFile="",
            wildcard="FoB file (*.fob)|*.fob", style=wx.SAVE | wx.CHANGE_DIR | wx.OVERWRITE_PROMPT)

            if dlg.ShowModal() == wx.ID_OK:                
                self.state.recordingSession.filename = dlg.GetPath()
                self.state.parent.recordingsProcessor.SaveRecording(dlg.GetPath(), self.state.recordingSession)
                self.state.parent.playbackState.addRecording(self.state.recordingSession)
                
            dlg.Destroy()
            
            fobfilepath = dlg.GetPath()
            matfilepath = fobfilepath.rstrip('.fob') + '.txt'
            fileWriter = open(matfilepath, 'w')
            for i in self.state.main.driver.rawoutputlist:
                fileWriter.write(i)
            fileWriter.close()
            self.state.main.driver.rawoutputlist = []                                    
        else:
            #~ dlg = wx.FileDialog(self.state.main.gui, message="Save as Raw Output file", defaultDir=os.getcwd(), defaultFile="",
            #~ wildcard="Txt-file (*.txt)|*.txt", style=wx.SAVE | wx.CHANGE_DIR | wx.OVERWRITE_PROMPT)
            #~ if dlg.ShowModal() == wx.ID_OK:
                #~ txtpath = dlg.GetPath()  
                #~ self.lastpath = txtpath
                #~ self.state.main.driver.fileWriter = open(txtpath, 'w')
            #~ dlg.Destroy()
            
            self.state.newRecordingSession()

            
            self.state.RECORDING = True

    ## Resets the camera.
    def OnResetCam(self, event):
        self.state.parent.ResetCamera(self.state)